package com.oa.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.components.Reset;
import org.aspectj.weaver.ast.And;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.stat.TableStat.Mode;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.oa.bean.TrSec;
import com.oa.bean.Trip;
import com.oa.bean.User;
import com.oa.service.TMService;
import com.oa.service.impl.TMServiceImpl;

@Controller
@RequestMapping("/showTr")
public class TripManage {
	
	@Autowired
	private TMServiceImpl tmService;

	/**
	 * 前台展示出差申请数据
	 * @param usp
	 * @param currentPage
	 * @return
	 */
	@RequestMapping("/showTrip")
	@ResponseBody
	public Map<String, Object> showTrip(String usp,Integer uid,Integer currentPage){
		System.out.println(usp);
		System.out.println(uid);
		int status=3;
		Trip trip=new Trip();
		if (uid!=0) {
			trip.setTr_exappuser_id(uid);
		}
		if (usp!=null && usp!="") {
			if (usp.equals("未审批")) {
				status=0;
				trip.setTr_status(status);
			}else if (usp.equals("审批通过")) {
				status=1;
				trip.setTr_status(status);
			}else if (usp.equals("审批不通过")) {
				status=2;
				trip.setTr_status(status);
			}else {
				trip.setTr_applyuser(usp);
				trip.setTr_status(status);
			}
		}else {
			trip.setTr_status(status);
		}
		System.out.println(trip);
		
		if(currentPage==0){
			currentPage=1;
		}
		System.out.println("currentPage="+currentPage);
		Page<PageInfo> pageInfo=PageHelper.startPage(currentPage, 8,"tr_id desc");
		List<TrSec> trSecs=tmService.findTr(trip);
		System.out.println(trSecs);
		Map<String, Object> map=new HashMap<>();
		map.put("trSecs", trSecs);
		map.put("pages", pageInfo.getPages());
		return map;
	}
	
	/**
	 * 根据id查询到数据，然后返回数据给审批页面显示
	 * @param tr_id
	 * @param model
	 * @return
	 */
	@RequestMapping("/findTr")
	@ResponseBody
	public Map<String, Object> findTr(int tr_id,Model model){
		TrSec trSec=tmService.findTrSec(tr_id);
		/*model.addAttribute("trSec", trSec);*/
		Map<String, Object> map=new HashMap<>();
		map.put("trSec", trSec);
		return map;
	}
	
	/**
	 * 审批修改状态
	 */
	@RequestMapping("/updateTr")
	public String updateTr(Model model,HttpServletRequest request){
		String btn=request.getParameter("btn");
		System.out.println(btn);
		int tr_id=Integer.parseInt(request.getParameter("hid"));
		System.out.println(tr_id);
		int status=0;
		if (btn.equals("审批通过")) {
			status=1;
		}else if(btn.equals("审批不通过")){
			status=2;
		}
		int result=tmService.updateTr(tr_id, status);
		
		return "../Template/showTrip";
		
	}
	
	/**
	 * 查看出差申请详细信息
	 * @param tr_id
	 * @param model
	 * @return
	 */
	@RequestMapping("/selectTr")
	@ResponseBody
	public Map<String, Object> selectTr(int tr_id,Model model){
		TrSec trSec=tmService.findTrSec(tr_id);
		System.out.println(trSec);
		//判断状态
		String status=null;
		if (trSec.getTr_status()==0) {
			status="未审批";
		}else if (trSec.getTr_status()==1) {
			status="审批通过";
		}else {
			status="审批未通过";
		}
		Map<String, Object> map=new HashMap<>();
		map.put("trSec", trSec);
		map.put("status", status);
		return map;
	}
	
	@RequestMapping("/deleteTr")
	@ResponseBody
	public Map<String, Object> deleteTr(int tr_id){
		System.out.println(tr_id);
		int result=tmService.deleteTr(tr_id);
		Map<String, Object> map=new HashMap<>();
		map.put("res", result);
		return map;
	}
	
	//删除  批量删除
	@RequestMapping("/deleteMore")
	@ResponseBody
	public Map<String,Object> deleteMore(String str){	
		String[] ptids=str.split(",");
		System.out.println(ptids);
		Map<String, Object> map=new HashMap<>();
		for (int i = 0; i < ptids.length; i++) {
			System.out.println(ptids[i]);
			int tr_id=Integer.parseInt(ptids[i]);
			int result=tmService.deleteTr(tr_id);
			if(result==1){				
				map.put("map1", result);
			}
		}
		return map;	
	}
	
	/**
	 * 审批人列表
	 * @return
	 */
	@RequestMapping("/findExuser")
	@ResponseBody
	public Map<String, Object> findExuser(){
		List<User> users=tmService.finduser();
		System.out.println(users);
		Map<String, Object> map=new HashMap<>();
		map.put("users", users);
		return map;
	}
	
	public Map<String, Object> Echart(){
		Map<String, Object> map=new HashMap<>();
		return map;
	}
}
